package bishi.day12_5_7;

/**
 * 二进制插入
 *
 * @author 是阿秋啊
 * @date 2022/05/07 16:46
 **/
public class BinInsert {
    public static int binInsert(int n, int m, int j, int i) {
        // write code here
        int count = 0;
        // temp用来存储n的第j位之前的二进制数
        int temp = 0;
        while (count < j) {
            temp |= (n & 1);
            n >>= 1;
            count++;
            if (count != j) {
                // 避免temp多进位一次
                temp <<= 1;
            }
        }
        n = n | m;
        for (int k = 0; k < count; k++) {
            n <<= 1;
            n |= (temp & 1);
            temp >>= 1;
        }
        return n;
    }

    public static void main(String[] args) {
        System.out.println(binInsert(1025, 18, 2, 6));
    }
}